﻿using FFmpeg.AutoGen.Abstractions;
using System.Diagnostics;
using System.Runtime.InteropServices;

namespace ymk_camera_surveillance.Decode;

public class InitFFmpeg
{
    public static InitFFmpeg instance = new InitFFmpeg();

    public InitFFmpeg()
    {
        if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
        {
            FFmpegBinariesHelper.RegisterFFmpegBinaries();
            prepareHWDecoder(out var decoderType);
        }
        else
            throw new NotSupportedException(); // fell free add support for platform of your choose
    }

    private void prepareHWDecoder(out AVHWDeviceType decoderType)
    {
        var availableHWDecoders = new Dictionary<int, AVHWDeviceType>();
        Debug.WriteLine("Select hardware decoder:");
        var type = AVHWDeviceType.AV_HWDEVICE_TYPE_NONE;
        var number = 0;

        while ((type = ffmpeg.av_hwdevice_iterate_types(type)) != AVHWDeviceType.AV_HWDEVICE_TYPE_NONE)
        {
            ++number;
            Debug.WriteLine($"{number}. {type} type enum {((int)type)} ");
            availableHWDecoders.Add(number, type);
        }
        decoderType = AVHWDeviceType.AV_HWDEVICE_TYPE_NONE;
        return;
    }
}
